package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.GettokenUtil;

import cn.wolfcode.luowowo.website.util.SettokenUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexColltroller {
  @Reference
  private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoSearchService;

    @Reference
    private IStrategyTemplateSearchService strategySearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("")  //获取用户对象
    @RequireLogin
    public String getuser(HttpServletRequest request,HttpServletResponse response,Model model){
        //获取token
        String tolen = GettokenUtil.getUser(request);
        //获取对象
        UserInfo userInfo = userInfoRedisService.gettoken(tolen);
        if(userInfo==null){
            throw new LogicException("登录失效或没登录！");
        }
        //延长cookie时间
        SettokenUtil.settoken(tolen,response);
        //延长redis时间
        userInfoRedisService.addtoken(tolen,userInfo);
        //共享数据
        request.getSession().setAttribute("userInfo",userInfo);
        //
        //显示前五tcs
        model.addAttribute("tcs",travelCommendService.getCommendTop5());

        //显示攻略推荐sc  拿一个显示页面就ok
        model.addAttribute("sc",strategyCommendService.getCommendTop5().get(0));
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo) {
        //pageInfo 分页操作
        model.addAttribute("pageInfo",travelService.query(qo));
        return "index/travelPageTpl";
    }
    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {

        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUserInfo(model,qo);
            default:return searchAll(model,qo);
        }
    }

    //目的地
    private String searchDest(Model model, SearchQueryObject qo) {
        //通过关键字查询目的地是否存在
        DestinationTemplate dest = destinationSearchService.getDestByName(qo.getKeyword());

        if(dest !=null){
            long total = 0;
            SearchResult data = new SearchResult();
            //存在:查询目的地下的下所有攻略， 游记， 用户 前五篇
            List<StrategyTemplate> str = strategySearchService.getByDestName(qo.getKeyword());
            //判断是否多余5个,如果多五就截取
            total += str.size();
            if (str.size() >5){

                str = str.subList(0,5);
            }
            List<TravelTemplate> tr = travelSearchService.getByDestName(qo.getKeyword());
            //判断是否多余5个,如果多五就截取
            total += tr.size();
            if (tr.size() >5){

                tr = tr.subList(0,5);
            }
            List<UserInfoTemplate> us = userInfoSearchService.getByDestName(qo.getKeyword());
            //判断是否多余5个,如果多五就截取
            total += us.size();
            if (us.size() >5){

                us = us.subList(0,5);
            }
            data.setStrategys(str);
            data.setTravels(tr);
            data.setUsers(us);
            data.setTotal(total);
            model.addAttribute("data",data);
        }

        //不存在的话 提示不存在
        model.addAttribute("dest",dest);
        return "index/searchDest";
    }

    //攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME
                , StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }

    //游记
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME
                , TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }

    //用户
    private String searchUserInfo(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME
                , UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }

    //所有
    private String searchAll(Model model, SearchQueryObject qo) {
        //游记
        Page<TravelTemplate> tr = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME
                , TravelTemplate.class, qo, "title", "summary");
        //攻略
        Page<StrategyTemplate> str = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME
                , StrategyTemplate.class, qo, "title", "summary");
        //用户
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME
                , UserInfoTemplate.class, qo, "nickname", "destName");
        //目的地
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME
                , DestinationTemplate.class, qo, "name", "info");
        SearchResult data = new SearchResult();
        data.setTotal(ds.getTotalElements()+str.getTotalElements()+tr.getTotalElements()+us.getTotalElements());

        data.setUsers(us.getContent());
        data.setTravels(tr.getContent());
        data.setStrategys(str.getContent());
        data.setDests(ds.getContent());

        model.addAttribute("data",data);

        return "index/searchAll";
    }
}
